import category,{type categoryModel} from '@/api/category';
import { ElMessage } from 'element-plus';
import { defineStore } from 'pinia';
interface useCategoryStoreModel{
  category1Id:undefined | number,
  category2Id:undefined | number,
  category3Id:undefined | number,
  category1List:categoryModel[],
  category2List:categoryModel[],
  category3List:categoryModel[]
}
const useCategoryStore = defineStore('category',{
  state:():useCategoryStoreModel=>({
    category1Id:undefined,
    category2Id:undefined,
    category3Id:undefined,
    category1List:[],
    category2List:[],
    category3List:[]
  }),
  actions:{
  async getCategory1List(){
      try {
        let result = await category.getCategory1List();
        // console.log(result);
        this.category1List = result;
      } catch (error) {
        console.log(error);
        ElMessage.error('获取一级分类失败，请重试');
      }
    },
    async getCategory2List(){
      try {
        let result = await category.getCategory2List(this.category1Id as number);
        // console.log(result);
        this.category2List = result;
      } catch (error) {
        console.log(error);
        ElMessage.error('获取2级分类失败，请重试');
      }
    },
    async getCategory3List(){
      try {
        let result = await category.getCategory3List(this.category2Id as number);
        // console.log(result);
        this.category3List = result;
      } catch (error) {
        console.log(error);
        ElMessage.error('获取3级分类失败，请重试');
      }
    },
  },
  getters:{}
})
export default useCategoryStore;